package com.example.canal.service.record;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.canal.conf.CanalConstants;
import com.example.canal.mapper.MqErrorRecordMapper;
import com.example.canal.model.MqErrorRecordPO;
import com.example.canal.model.dto.SaveMqErrorRecordDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MqErrorRecordServiceImpl extends
        ServiceImpl<MqErrorRecordMapper, MqErrorRecordPO> implements MqErrorRecordService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMqErrorRecords(List<SaveMqErrorRecordDTO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<MqErrorRecordPO> records = new ArrayList<>();
        for (SaveMqErrorRecordDTO recordDTO : list) {
            MqErrorRecordPO recordPO = new MqErrorRecordPO();
            recordPO.setErrorRecord(recordPO.getErrorRecord());
            recordPO.setCreateTime(new Date());
            recordPO.setCreateUsername(CanalConstants.DEFAULT_USERNAME);
            recordPO.setCreateUserid(CanalConstants.DEFAULT_USERID);
            recordPO.setErrorException(recordDTO.getErrorException());
            recordPO.setStatus(CanalConstants.MQ_ERROR_STATUS_FAIL);
            recordPO.setCurrentHandlerName(recordDTO.getApplicationName());
            recordPO.setNextDestination(recordDTO.getNextDestination());
            recordPO.setMessageId(recordDTO.getMessageId());
            records.add(recordPO);
        }
        this.saveBatch(records);
    }
}
